package com.lzp.common.config;

import org.flywaydb.core.Flyway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.nio.charset.StandardCharsets;

/**
 * @author lzp
 * @version 1.0.0
 * @date 2022/5/17 17:11:31
 * @apiNote
 */
@Configuration
public class FlywayConfig {
    @Autowired
    private DataSource dataSource;

    /**
     * @see @PostConstruct注释需要设置flyway不默认启动,待所有bean加载完成后初始化
     */
    @Bean
    public Flyway migrate() {
        Flyway flyway = new Flyway();
        //巨坑!sql文件命名要规范,V后边加版本,再用俩下划线间隔,间隔后可以写追加注释信息,例如表名
        flyway.setDataSource(dataSource);
        flyway.setLocations("classpath:flyway/sql");
        flyway.setBaselineOnMigrate(true);
        flyway.setEncoding(StandardCharsets.UTF_8.toString());
        flyway.setTable("flyway_schema_history_wechat_bill");
        flyway.migrate();
        return flyway;
    }
}
